package leveldesigner.gui.map;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class NewMap extends JDialog implements ActionListener{
	
	private JPanel panel;
	private JTextField tfName;
	private JTextField tfWidth;
	private JTextField tfHeight;
	private JButton btnOK;
	private JButton btnCancel;
	private String mapName;
	private int mapWidth;
	private int mapHeight;
	private boolean mapCreated;
	
	public NewMap(Container parent){
		panel = new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Map"));
		mapCreated = false;
		this.setTitle("Create New Map");
		this.setModal(true);
		this.setSize(300,160);
		this.setLocationRelativeTo(parent);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.create();
		this.setVisible(true);
	}
	
	private void create(){
		
		// Initialize Components
		JLabel lblName = new JLabel("Map Name: ");
		tfName = new JTextField();
		
		JLabel lblWidth = new JLabel("Width: ");
		tfWidth = new JTextField();
		
		JLabel lblHeight = new JLabel("Height: ");
		tfHeight = new JTextField();
		
		btnOK = new JButton("OK");
		btnOK.setAlignmentX(CENTER_ALIGNMENT);
		btnOK.setMaximumSize(new Dimension(80,25));
		btnOK.setPreferredSize(new Dimension(80,25));
		btnOK.addActionListener(this);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setAlignmentX(CENTER_ALIGNMENT);
		btnCancel.setMaximumSize(new Dimension(80,25));
		btnCancel.setPreferredSize(new Dimension(80,25));
		btnCancel.addActionListener(this);
		
		// Set GridBagLayout as main Layout for the panel
		panel.setLayout(new GridBagLayout());
		// Create GridBagConstraints to control components
		GridBagConstraints gbc = new GridBagConstraints();
		// Add components to the grid
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 0;
		gbc.insets = new Insets(0, 10, 10, 0);
		panel.add(lblName, gbc);
		
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		gbc.weightx = 1;
		gbc.insets = new Insets(0, 0, 0, 10);
		panel.add(tfName, gbc);
		
		gbc.insets = new Insets(0, 10, 0, 0);
		gbc.gridy = 1;
		gbc.gridx = 0;
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		panel.add(lblWidth, gbc);
		
		gbc.insets = new Insets(0, 0, 0, 10);
		gbc.gridx = 1;
		gbc.weightx = 1;
		panel.add(tfWidth, gbc);
		
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.gridx = 2;
		gbc.weightx = 0;
		panel.add(lblHeight, gbc);
		
		gbc.insets = new Insets(0, 20, 0, 60);
		gbc.gridx = 3;
		gbc.weightx = 1;
		panel.add(tfHeight, gbc);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.setAlignmentX(CENTER_ALIGNMENT);
		buttonPanel.add(btnOK);
		buttonPanel.add(Box.createRigidArea(new Dimension(5,0)));
		buttonPanel.add(btnCancel);
		
		gbc.insets = new Insets(25,0,0,0);
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 4;
		panel.add(buttonPanel, gbc);
		
		this.add(panel);
	}

	public String getMapName() {
		return mapName;
	}

	public void setMapName(String mapName) {
		this.mapName = mapName;
	}

	public int getMapWidth() {
		return mapWidth;
	}

	public void setMapWidth(int mapWidth) {
		this.mapWidth = mapWidth;
	}

	public int getMapHeight() {
		return mapHeight;
	}

	public void setMapHeight(int mapHeight) {
		this.mapHeight = mapHeight;
	}
	
	public boolean isMapCreated(){
		return mapCreated;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnOK)){
			if (!tfName.getText().trim().isEmpty()){
				try{
					mapName = tfName.getText();
					mapWidth = Integer.parseInt(tfWidth.getText());
					mapHeight = Integer.parseInt(tfHeight.getText());
					mapCreated = true;
					setVisible(false);
				}catch (NumberFormatException nfe){
					JOptionPane.showMessageDialog (this, "Width and Height must be numeric values.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}else{
				JOptionPane.showMessageDialog(this,"Map name cannot be empty.", "Error" , JOptionPane.ERROR_MESSAGE);
			}
		}else if (e.getSource().equals(btnCancel)){
			this.dispose();
		}
	}		
}
